import ResMgr from "./ResMgr";

const {ccclass, property} = cc._decorator;

export enum UIType  {
    Full = 0,           //全屏UI
    UnFull = 1,         //非全屏
    Up = 2,             //弹框层
}

@ccclass
export default class UIMgr extends cc.Component {
    public static Instance: UIMgr = null;
    onLoad() {
        if (UIMgr.Instance !== null) {
            this.destroy();
            return;
        }
        UIMgr.Instance = this;
    }

    start() {

    }

    public Init(): void {
        console.log("========UIMgr Init================");
        
    }

    /**
     * 
     * @param abName ab包名字
     * @param ui_path UI路径
     * @param assetType 资源类型
     * @param parentType 父节点
     * @returns 
     */
    public async ShopwUIAt(abName: string, ui_path:string, assetType:any,parentType:number = UIType.Full) {
        var prefab = await ResMgr.Instance.IE_GetAsset(abName,ui_path, assetType);
        // cc.log("=========UIMgr ShopwUIAt==========",prefab);
        var item_ts = null;
        if (prefab) {
            let item:any = cc.instantiate(prefab);
            let parent = this.GetParent(parentType);
            parent.addChild(item);
            item_ts = item.addComponent(item.name + "_Ctr");
        }else{
            cc.error("ui_path ", ui_path, " not in ab name of  ", abName);
        }
        return item_ts;
    }


    //获取父节点
    public GetParent(parentType: number): cc.Node {
        let canvas = cc.find("Canvas");
        let item = null;
        switch (parentType) {
            case UIType.Full:
                item = canvas.getChildByName("Full");
                break;
            case UIType.UnFull:
                item = canvas.getChildByName("UnFull");
                break;
            case UIType.Up:
                item = canvas.getChildByName("Up");
                break;
            default:
                item = canvas;
                break;
        }
        return item;
    }

}
